"""全局配置定义."""

import os


class Config:
    """基础配置类."""

    HOST = os.getenv("HOST", "0.0.0.0")
    PORT = int(os.getenv("PORT", 3003))
    DEBUG = os.getenv("DEBUG", "False").lower() == "true"

    SECRET_KEY = os.getenv("SECRET_KEY", "erp_mock_secret_key_2023")

    LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
    LOG_FILE = os.getenv("LOG_FILE", "logs/erp_mock.log")

    API_VERSION = "v1.0"
    API_PREFIX = "/yonbip"

    TOKEN_EXPIRE_SECONDS = int(os.getenv("TOKEN_EXPIRE_SECONDS", 7200))

    APP_KEY = os.getenv("APP_KEY", "6b539a1691a4461dbabd3c359a06eb2d")
    APP_SECRET = os.getenv(
        "APP_SECRET",
        "def03b69c1890b617b203ea656ed9e5528c14bbe",
    )
    APP_NAME = os.getenv("APP_NAME", "MES集成应用")
    TENANT_ID = os.getenv("TENANT_ID", "")

    SKIP_SIGNATURE_VERIFY = os.getenv("SKIP_SIGNATURE_VERIFY", "False").lower() == "true"
    MOCK_DATA_ENABLED = os.getenv("MOCK_DATA_ENABLED", "True").lower() == "true"
    MOCK_SUCCESS_RATE = float(os.getenv("MOCK_SUCCESS_RATE", 0.95))

    SYSTEM_NAME = "用友 ERP 模拟服务"
    SYSTEM_VERSION = "U9Cloud 2023"
    SYSTEM_ENVIRONMENT = os.getenv("SYSTEM_ENVIRONMENT", "测试环境")


class DevelopmentConfig(Config):
    """开发环境配置."""

    DEBUG = True
    HOST = "127.0.0.1"
    PORT = 3003


class ProductionConfig(Config):
    """生产环境配置."""

    DEBUG = False
    PORT = 3003
    LOG_LEVEL = "WARNING"


class TestingConfig(Config):
    """测试环境配置."""

    DEBUG = True
    MOCK_SUCCESS_RATE = 1.0


config_map = {
    "development": DevelopmentConfig,
    "production": ProductionConfig,
    "testing": TestingConfig,
    "default": DevelopmentConfig,
}


def get_config(config_name: str = None) -> Config:
    """根据名称返回配置类."""

    config_name = config_name or os.getenv("FLASK_CONFIG", "default")
    return config_map.get(config_name, DevelopmentConfig)

